package com.atlin.demo_client.config;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 文件同步配置助手
 */
@Slf4j
public class ConfigInfoHelper {

    private static ConfigInfo configInfo;

    public static void init() {
        String configPath = System.getProperty("user.dir");
        // 读取文件内容为字符串
        String jsonStr = FileUtil.readUtf8String(new File(configPath + File.separator + "config.json"));

        // 方式1：直接转成对象
        configInfo = JSONUtil.toBean(jsonStr, ConfigInfo.class);
        log.info("文件同步配置: {}", configInfo);
    }

    public static ConfigInfo getConfigInfo() {
        return configInfo;
    }

    public static List<ConfigInfo.DirectoryInfo> getDirectoryInfoList() {
        return configInfo.getDirectoryInfoList();
    }

    public static List<String> getNeedSyncDirectoryList() {
        return configInfo.getDirectoryInfoList().stream()
                .map(ConfigInfo.DirectoryInfo::getSyncDirectory)
                .collect(Collectors.toList());
    }

    public static String getStorageDir() {
        return configInfo.getStorageDir();
    }

    public static Set<String> getDirectoryPrefixSet() {
        return configInfo.getDirectoryInfoList()
                .stream().map(ConfigInfo.DirectoryInfo::getDirectoryPrefix)
                .collect(Collectors.toSet());
    }

}
